package com.zhang.servlet.publish;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhang.service.AlbumService;
import com.zhang.util.Log;
import com.zhang.view_object.publish.PlayAlbumView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author happy
 **/
@WebServlet("/album-detail.json")
public class AlbumDetailServlet extends HttpServlet {
    private final AlbumService albumService = new AlbumService();
    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Log.println("访问/album-detail.json");
        req.setCharacterEncoding("utf-8");
        String aid = req.getParameter("aid");
        PlayAlbumView playAlbumView = albumService.getAlbumDetail(aid);

        String json = objectMapper.writeValueAsString(playAlbumView);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        resp.getWriter().println(json);
        Log.println("响应JSON已发送");
    }
}